package com.sinothk.fc.bizType.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinothk.fc.bizType.entity.BizTypeEntity;
import com.sinothk.fc.bizType.entity.BizTypeSearchVO;
import com.sinothk.fc.bizType.service.IBizTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 行业分类
 * @Author: Liangyt
 * @Date: 2022-11-11
 * @Version: V1.0
 */
@Api(tags = "行业分类")
@RestController
@RequestMapping("/fc/bizTypeCtrl")
@Slf4j
public class BizTypeController extends JeecgController<BizTypeEntity, IBizTypeService> {

    private final IBizTypeService bizTypeService;

    public BizTypeController(IBizTypeService bizTypeService) {
        this.bizTypeService = bizTypeService;
    }

    /**
     * 分页列表查询
     *
     * @param bizType
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "行业分类-分页列表查询")
    @ApiOperation(value = "行业分类-分页列表查询", notes = "行业分类-分页列表查询")
    @GetMapping(value = "/rootList")
    public Result<?> queryPageList(BizTypeEntity bizType,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        String hasQuery = req.getParameter("hasQuery");
        if ("true".equals(hasQuery)) {
            QueryWrapper<BizTypeEntity> queryWrapper = QueryGenerator.initQueryWrapper(bizType, req.getParameterMap());
            queryWrapper.eq("pid", "0");
            List<BizTypeEntity> list = bizTypeService.queryTreeListNoPage(queryWrapper);
            IPage<BizTypeEntity> pageList = new Page<>(1, 10, list.size());
            pageList.setRecords(list);
            return Result.OK(pageList);
        } else {
            String parentId = bizType.getPid();
            if (oConvertUtils.isEmpty(parentId)) {
                parentId = "0";
            }
            bizType.setPid(null);
            QueryWrapper<BizTypeEntity> queryWrapper = QueryGenerator.initQueryWrapper(bizType, req.getParameterMap());
            // 使用 eq 防止模糊查询
            queryWrapper.eq("pid", parentId);
            Page<BizTypeEntity> page = new Page<BizTypeEntity>(pageNo, pageSize);
            IPage<BizTypeEntity> pageList = bizTypeService.page(page, queryWrapper);
            return Result.OK(pageList);
        }
    }

    /**
     * 获取子数据
     *
     * @param bizType
     * @param req
     * @return
     */
    @AutoLog(value = "行业分类-获取子数据")
    @ApiOperation(value = "行业分类-获取子数据", notes = "行业分类-获取子数据")
    @GetMapping(value = "/childList")
    public Result<?> queryPageList(BizTypeEntity bizType, HttpServletRequest req) {
        QueryWrapper<BizTypeEntity> queryWrapper = QueryGenerator.initQueryWrapper(bizType, req.getParameterMap());
        List<BizTypeEntity> list = bizTypeService.list(queryWrapper);
        IPage<BizTypeEntity> pageList = new Page<>(1, 10, list.size());
        pageList.setRecords(list);
        return Result.OK(pageList);
    }

    /**
     * 批量查询子节点
     *
     * @param parentIds 父ID（多个采用半角逗号分割）
     * @param parentIds
     * @return
     */
    @AutoLog(value = "行业分类-批量获取子数据")
    @ApiOperation(value = "行业分类-批量获取子数据", notes = "行业分类-批量获取子数据")
    @GetMapping("/getChildListBatch")
    public Result getChildListBatch(@RequestParam("parentIds") String parentIds) {
        try {
            QueryWrapper<BizTypeEntity> queryWrapper = new QueryWrapper<>();
            List<String> parentIdList = Arrays.asList(parentIds.split(","));
            queryWrapper.in("pid", parentIdList);
            List<BizTypeEntity> list = bizTypeService.list(queryWrapper);
            IPage<BizTypeEntity> pageList = new Page<>(1, 10, list.size());
            pageList.setRecords(list);
            return Result.OK(pageList);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("批量查询子节点失败：" + e.getMessage());
        }
    }

    /**
     * 添加
     *
     * @param bizType
     * @return
     */
    @AutoLog(value = "行业分类-添加")
    @ApiOperation(value = "行业分类-添加", notes = "行业分类-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody BizTypeEntity bizType) {
        bizTypeService.addBizType(bizType);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param bizType
     * @return
     */
    @AutoLog(value = "行业分类-编辑")
    @ApiOperation(value = "行业分类-编辑", notes = "行业分类-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody BizTypeEntity bizType) {
        bizTypeService.updateBizType(bizType);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "行业分类-通过id删除")
    @ApiOperation(value = "行业分类-通过id删除", notes = "行业分类-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        bizTypeService.deleteBizType(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "行业分类-批量删除")
    @ApiOperation(value = "行业分类-批量删除", notes = "行业分类-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.bizTypeService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "行业分类-通过id查询")
    @ApiOperation(value = "行业分类-通过id查询", notes = "行业分类-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        BizTypeEntity bizType = bizTypeService.getById(id);
        if (bizType == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(bizType);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param bizType
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BizTypeEntity bizType) {
        return super.exportXls(request, bizType, BizTypeEntity.class, "行业分类");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BizTypeEntity.class);
    }

    /**
     * 获取大类列表
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @AutoLog(value = "行业分类-获取大类列表")
    @ApiOperation(value = "行业分类-获取大类列表", notes = "行业分类-获取大类列表")
    @GetMapping(value = "/bigBizTypeList")
    public Result<IPage<BizTypeEntity>> bigBizTypeList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return Result.OK(bizTypeService.bigBizTypeList(new Page<>(pageNo, pageSize)));
    }

    /**
     * 分页列表查询
     *
     * @param searchVO
     * @param pageNo
     * @param pageSize
     * @return
     */
    @AutoLog(value = "分页列表查询")
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @GetMapping(value = "/listForLeftOrRight")
    public Result<?> listForLeftOrRight(
            BizTypeSearchVO searchVO,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<BizTypeEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(searchVO.getPid())) {
            queryWrapper.eq(BizTypeEntity::getPid, searchVO.getPid());
        }

        if (StringUtils.isNotEmpty(searchVO.getTradeName())) {
            queryWrapper.like(BizTypeEntity::getName, searchVO.getTradeName());
        }
        queryWrapper.eq(BizTypeEntity::getFlag, "0"); // 分类种类：0商品分类1展示分类
        queryWrapper.orderByAsc(BizTypeEntity::getSort);

        IPage<BizTypeEntity> pageList = bizTypeService.pageList(new Page<>(pageNo, pageSize), queryWrapper);
        if (pageList.getRecords().size() > 0) {
            pageList.getRecords().get(0).setSelected(true);
        }
        return Result.OK(pageList);
    }
}
